/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.workflow.service.impl;

import me.zhengjie.modules.workflow.domain.ActReProcdef;
import me.zhengjie.modules.workflow.service.ActReDeploymentService;
import me.zhengjie.modules.workflow.service.dto.ActReDeploymentDto;
import me.zhengjie.modules.workflow.service.dto.ActReDeploymentQueryCriteria;
import me.zhengjie.modules.workflow.service.dto.ActReProcdefDto;
import me.zhengjie.modules.workflow.service.dto.ActReProcdefQueryCriteria;
import me.zhengjie.utils.*;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.workflow.repository.ActReProcdefRepository;
import me.zhengjie.modules.workflow.service.ActReProcdefService;
import me.zhengjie.modules.workflow.service.mapstruct.ActReProcdefMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.hutool.core.util.IdUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author wzp
* @date 2021-04-19
**/
@Service
@RequiredArgsConstructor
public class ActReProcdefServiceImpl implements ActReProcdefService {

    private final ActReProcdefRepository actReProcdefRepository;
    private final ActReProcdefMapper actReProcdefMapper;
    private final ActReDeploymentService actReDeploymentService;
    @Override
    public Map<String,Object> queryAll(ActReProcdefQueryCriteria criteria, Pageable pageable){
        //1、查询流程分类相关的部署
        String categoryId = criteria.getCategoryId();
        ActReDeploymentQueryCriteria actReDeploymentQueryCriteria = new ActReDeploymentQueryCriteria();
        actReDeploymentQueryCriteria.setCategory(categoryId);
        List<ActReDeploymentDto> actReDeploymentDtos = actReDeploymentService.queryAll(actReDeploymentQueryCriteria);
        final List<String> deploymentIds = new ArrayList<String>();
        if(!actReDeploymentDtos.isEmpty()){
            actReDeploymentDtos.forEach(item ->{
                deploymentIds.add(item.getId());
            });
        }
        //设置in查询
        criteria.setDeploymentId(deploymentIds);
        Page<ActReProcdef> page = actReProcdefRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(actReProcdefMapper::toDto));
    }

    @Override
    public List<ActReProcdefDto> queryAll(ActReProcdefQueryCriteria criteria){
        return actReProcdefMapper.toDto(actReProcdefRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public ActReProcdefDto findById(String id) {
        ActReProcdef actReProcdef = actReProcdefRepository.findById(id).orElseGet(ActReProcdef::new);
        ValidationUtil.isNull(actReProcdef.getId(),"ActReProcdef","id",id);
        return actReProcdefMapper.toDto(actReProcdef);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ActReProcdefDto create(ActReProcdef resources) {
        resources.setId(IdUtil.simpleUUID()); 
        return actReProcdefMapper.toDto(actReProcdefRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(ActReProcdef resources) {
        ActReProcdef actReProcdef = actReProcdefRepository.findById(resources.getId()).orElseGet(ActReProcdef::new);
        ValidationUtil.isNull( actReProcdef.getId(),"ActReProcdef","id",resources.getId());
        actReProcdef.copy(resources);
        actReProcdefRepository.save(actReProcdef);
    }

    @Override
    public void deleteAll(String[] ids) {
        for (String id : ids) {
            actReProcdefRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<ActReProcdefDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (ActReProcdefDto actReProcdef : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put(" rev",  actReProcdef.getRev());
            map.put(" category",  actReProcdef.getCategory());
            map.put(" name",  actReProcdef.getName());
            map.put(" key",  actReProcdef.getKey());
            map.put(" version",  actReProcdef.getVersion());
            map.put(" deploymentId",  actReProcdef.getDeploymentId());
            map.put(" resourceName",  actReProcdef.getResourceName());
            map.put(" dgrmResourceName",  actReProcdef.getDgrmResourceName());
            map.put(" description",  actReProcdef.getDescription());
            map.put(" hasStartFormKey",  actReProcdef.getHasStartFormKey());
            map.put(" hasGraphicalNotation",  actReProcdef.getHasGraphicalNotation());
            map.put(" suspensionState",  actReProcdef.getSuspensionState());
            map.put(" tenantId",  actReProcdef.getTenantId());
            map.put(" engineVersion",  actReProcdef.getEngineVersion());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}